package com.wx.learn;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

/**
 * ArrayList使用
 * 存储结构：数组,查询快,增删慢
 */
public class ArrayListDemo5 {
    public static void main(String[] args) {
        // 创建集合
        ArrayList arrayList = new ArrayList();
        // 1.添加元素
        collectionDemo2Student s1 = new collectionDemo2Student("zhang1", 20);
        collectionDemo2Student s2 = new collectionDemo2Student("zhang2", 30);
        collectionDemo2Student s3 = new collectionDemo2Student("zhang3", 40);

        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        System.out.println(arrayList.size()+" "+arrayList);
        // 2.删除元素
//        arrayList.remove(0);
//        arrayList.remove(s1);
        // 重写equals
//        arrayList.remove(new collectionDemo2Student("zhang1", 20));
//        System.out.println(arrayList);

        // 3.遍历元素
        // 3-1.使用迭代器遍历
        System.out.println("----------3-1.使用迭代器遍历----------");
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            collectionDemo2Student s= (collectionDemo2Student)it.next();
            System.out.println(s);
        }
        // 3-2.列表迭代器
        System.out.println("----------3-2.列表迭代器逆序----------");
        ListIterator itl = arrayList.listIterator();
        while (itl.hasNext()) {
            collectionDemo2Student sl = (collectionDemo2Student)itl.next();
            System.out.println(sl);
        }
        System.out.println(itl.hasPrevious());
        while (itl.hasPrevious()) {
            collectionDemo2Student sl = (collectionDemo2Student)itl.previous();
            System.out.println(sl);
        }
        // 4.判断
        System.out.println("----------4.判断----------");
        System.out.println(arrayList.contains(new collectionDemo2Student("zhang1", 20)));
        System.out.println(arrayList.contains(s1));
        System.out.println(arrayList.isEmpty());
        // 5.查找
        System.out.println(arrayList.indexOf(s1));
        System.out.println(arrayList.indexOf(new collectionDemo2Student("zhang1", 20)));
    }
}
